var addonReq = new Array();
var buzztimer = new Array();
var stretchobjecttimer = null;
var stretching = false;
var shrinkobjecttimer = null;
var shrinking = false;
var adminback = '';


function js_checkallselected(formobj) {
    var onoff = true; var n=0;            
    for(i=0; i<formobj.elements.length; i++) {
        var obj = formobj.elements[i];
        if(obj.name.indexOf('check_')==-1) continue;    
        if(!obj.checked) onoff = false;
        n++;
    }
    if(n==0) onoff = false;
    getObject('selectallcheckbox').checked = onoff;
}
function js_selectall(formobj,onoff) {
    for(i=0; i<formobj.elements.length; i++) {
        var obj = formobj.elements[i];
        if(obj.name.indexOf('check_')==-1) continue;
        js_addID(formobj,parseInt(obj.name.replace('check_','')),onoff,0);
    }
    js_updateselected(formobj);
}
function js_addID(formobj,ID,onoff,cas) {
    if(undefined===cas) cas=1;
    if(!getObject('check_'+ID)) return;
    if(onoff) {
        if(js_getindex(IDs_selected,ID)==false) {
            IDs_selected[IDs_selected.length] = ID;
        }
    } else {
        IDs_selected.splice(js_getindex(IDs_selected,ID),1);
        getObject('selectallcheckbox').checked = false;
    }
    getObject('check_'+ID).checked = onoff;
    
    if(cas==1) {
        js_checkallselected(formobj);
        js_updateselected(formobj);
    }
}
function js_updateselected(formobj) {
    var obj = getObject(formobj.name+'_updateselectedspan');
    var length = IDs_selected.length;
    if(length==0) obj.innerHTML = '';
    else obj.innerHTML = '(Selected: <b>'+length+'</b>)';
}
function js_getindex(arr,val) {
    for(var i=0; i<arr.length; i++) {if(arr[i]==val) return i;}
    return false;
}
     
function doadminback() {
    if(adminback=='') document.location.href='/pages/admin/dashboard';
    else {
        var tmp = adminback.split('(');
        tmp[1] = tmp[1].replace(')','');
        while(tmp[1].indexOf('\'')!=-1) tmp[1] = tmp[1].replace('\'','');
        while(tmp[1].indexOf('"')!=-1) tmp[1] = tmp[1].replace('"','');
        try{window[tmp[0]](tmp[1]);} catch(e){}
    }
}

function lightbox(openclose,shortname) {
    if(!openclose) return;
    if(openclose=='open') {
        gototop();
        getObject('html').style.overflowY = 'hidden';
        getObject('html').style.overflowX = 'hidden';
        getObject('html').style.paddingRight = '17';
        getObject('lightbox_main').style.display = 'block';
        getObject('lightbox_inner').style.display = 'block';
        if(shortname=='' || shortname===undefined) getaddon('/pages/@lightbox.php','shortname='+shortname,'lightboxshowdiv')
    } else {
        getObject('lightbox_main').style.display = 'none';
        getObject('lightbox_inner').style.display = 'none';
        getObject('html').style.overflowY = 'scroll';
        getObject('html').style.overflowX = '';
        getObject('html').style.paddingRight = '0';
    }
}

function getajaxlist(type,div,callback,callback_param,text,input_id) {
    getObject(div).style.display='';
    var params = '';
    params += 'type='+type;
    params += '&text='+text;
    params += '&div='+div;
    params += '&callback='+callback;
    params += '&callback_param='+callback_param;
    params += '&input_id='+input_id;
    getaddon('/pages/@suggest',params,div);
}

function doadminmemberlogin(p) {
    if(p===undefined || p=='') return;
    if(!document.adminmemberloginform.adminmemberlogin) return;
    var member = document.adminmemberloginform.adminmemberlogin.value;
    getaddon('/pages/register','doadminmemberlogin=1&member='+member,'spacerdiv','doadminmemberlogin_callback');    
}
function doadminmemberlogin_callback(status) {
    if(callbackerror(status)) return;
}

function reloadaccountbalance(showdiv) {
    showdiv = (showdiv===undefined) ? 'accountbalancediv' : showdiv;
    getaddon('/','reloadaccountbalance=1',showdiv);
}
function reloadpurchasebalance(showdiv) {
    showdiv = (showdiv===undefined) ? 'purchasebalancediv' : showdiv;
    getaddon('/','reloadpurchasebalance=1',showdiv);
}
function reloadnewmessages(showdiv) {
    showdiv = (showdiv===undefined) ? 'headernewmessagesdiv' : showdiv;
    getaddon('/','reloadnewmessages=1',showdiv);
}

function startloadingdots(obj) {
    obj.innerHTML += (obj.innerHTML=='....') ? '.' : obj.innerHTML+'.';    
}

function isMouseLeave(e,handler) {
    if (e.type != 'mouseout' && e.type != 'mouseover') return false;
    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
    var found = false;
    while (reltg && !found) {
        if(reltg == handler) return true;
        reltg = reltg.parentNode;
    }
    return false;
}

function stretchobject(obj_id,newheight) {
    if(shrinking) return;
    var obj = getObject(obj_id);
    if(!obj) return;
    shrinkobjecttimer = null;
    var curheight = parseInt(obj.style.height);
    if(curheight>=newheight) {stretching=false; stretchobjecttimer=null; return;}
    stretching = true;
    var increment = 10;
    if((newheight-curheight)<10) increment = 1;
    else if((newheight-curheight)<30) increment = 3;
    stretchobjecttimer = window.setTimeout(function() {
        getObject(obj_id).style.height = parseInt(getObject(obj_id).style.height) + increment;
        stretchobject(obj_id,newheight);
    },10);
}
function shrinkobject(obj_id,newheight) {
    if(stretching) stretchobjecttimer = null;
    var obj = getObject(obj_id);
    if(!obj) return;
    var curheight = parseInt(obj.style.height);
    if(curheight<=newheight) {shrinking=false; shrinkobjecttimer=null; return;}
    shrinking = true;
    var decrement = 10;
    if((curheight-newheight)<10) decrement = 1;
    else if((curheight-newheight)<30) decrement = 3;
    shrinkobjecttimer = window.setTimeout(function() {
        getObject(obj_id).style.height = parseInt(getObject(obj_id).style.height) - decrement;
        shrinkobject(obj_id,newheight);
    },10);
}

function getaddon(url,parameters,showdiv,callback) {
    addonReq[showdiv] = null;
    addonReq[showdiv] = getXMLDoc();
    if(getObject('icon_processing'+showdiv)) getObject('icon_processing'+showdiv).style.visibility = 'visible';
    addonReq[showdiv].open("POST",url,true);
    addonReq[showdiv].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    addonReq[showdiv].setRequestHeader("Content-length", parameters.length);
    addonReq[showdiv].setRequestHeader("Connection", "close");
    addonReq[showdiv].onreadystatechange = function() {
        if(addonReq[showdiv]) {
            if(addonReq[showdiv].readyState==4 && addonReq[showdiv].status==200) {
                var nodename = getObject(showdiv).nodeName;
                var response = addonReq[showdiv].responseText.split('-=-=-');
                if(nodename=="DIV") {
                    getObject(showdiv).innerHTML  = "<div id=icon_processing"+showdiv+" style='position:relative; visibility:hidden;'><div style='position:absolute; top:0%; left:50%;'><img src='Image/icon_processing.gif' /></div></div>";
                    getObject(showdiv).innerHTML += response[0];
                } else if(nodename=="INPUT") {
                    getObject(showdiv).value = response[0];
                } else if(nodename=="TEXTAREA") {
                    getObject(showdiv).value = response[0];
                } else if(nodename=="IMAGE") {
                    getObject(showdiv).src = response[0];
                } else if(nodename=="SPAN") { //no icon
                    getObject(showdiv).innerHTML = response[0];
                } else if(nodename=="IMG") {
                    getObject(showdiv).src = response[0];
                }
                if(window[callback]) {                    
                    window[callback](response[1]);
                }
            }
        }
    }
    addonReq[showdiv].send(parameters);
}

function gototop() {
    scroll(0,0);
}

function wrapformdata(form) {
    var params = '';
    for(i=0; i<form.elements.length; i++) {
        var obj = form.elements[i];
        if(obj.nodeName=='FIELDSET') continue;
        if(obj.type=='radio')
        {
	//		var name = (obj.type=='radio') ? obj.id : obj.name;
			var name = obj.id;
			var value = obj.value;
        }
        else if(obj.type=='checkbox')
        {
            var name = obj.name;
            var value = obj.value;
        }
        else
        {
            var name = obj.name;
            var value = obj.value.replace('&','%26');
		}
        if(name=='') continue;

        params += '&'+name+'='+value;
    }
    return params;
}
      
function getXMLDoc() {
    if(window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function getObject(elementID) {
    if (document.getElementById)
        return document.getElementById(elementID);
    else if (document.layers)
        return document.layers[elementID];
    else if (document.all)
        return document.all[elementID];
}

function buzz(obj,clear) {
    if(obj===undefined) return;
    if(clear===undefined) clear = false;
    if(buzztimer[obj.name]) window.clearTimeout(buzztimer[obj.name]);
    if(buzztimer[obj.id]) window.clearTimeout(buzztimer[obj.id]);
    obj.style.background = '#CC4444';
    obj.style.color = 'white';
    var tmpname = (obj.name=='') ? obj.id : obj.name;
    buzztimer[tmpname] = setTimeout(function() {
        if(obj.nodeName=='LABEL') obj.style.background = 'white';
        else if(obj.nodeName=='DIV') obj.style.background = '';
        else obj.style.background = '';
        obj.style.color='';
        if(obj.value && clear) obj.value='';
        buzztimer[tmpname] = null;
    },3000);
}

function callbackerror(status) {
    if(status===undefined) return false;
    status = status.split('###');
    if(status[0]=='ERR0') return true; /*message is shown*/
    if(status[0]=='ERR1') {getObject(status[1]).focus(); buzz(getObject(status[1])); return true;}
    if(status[0]=='RELOAD') {window.location.reload(); return true;}
    return false;
}

function hl(on,obj,back) {
    if(!obj) return;
    if(back===undefined || back=='') back = 'white';
    obj.style.background = (on==1) ? '#E9EBF2' : back; //DDE0ED
    obj.style.color = (on==1) ? '#2d2d2d' : '#4d4d4d';
}

function number_format(a,b) {
     if(!isDecimal(a)) return a;
     var b = (b===undefined) ? 2 : b;
     c = '.';
     d = ',';
     a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
     e = a + '';
     f = e.split('.');
     if (!f[0]) {
      f[0] = '0';
     }
     if (!f[1]) {

      f[1] = '';
     }
     if (f[1].length < b) {
      g = f[1];
      for (i=f[1].length + 1; i <= b; i++) {
       g += '0';
      }
      f[1] = g;
     }
     if(d != '' && f[0].length > 3) {
      h = f[0];
      f[0] = '';
      for(j = 3; j < h.length; j+=3) {
       i = h.slice(h.length - j, h.length - j + 3);
       f[0] = d + i +  f[0] + '';
      }
      j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
      f[0] = j + f[0];
     }
     c = (b <= 0) ? '' : c;
     return f[0] + c + f[1];
}

function isInteger(s) {
    if(s=='') return false;
    var valid = '0123456789';
    for(var i=0; i<s.length; i++)
        if(valid.indexOf(s.charAt(i)) < 0) return false;
    return true;
}

function isDecimal(s) {
    if(s=='') return false;
    var valid = '0123456789.';
    for(var i=0; i<s.length; i++)
        if(valid.indexOf(s.charAt(i)) < 0) return false;
    return true;
}